<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ObjectStore Allocation Tests</title>

<script type="text/javascript" src="../lib/jsunit/app/jsUnitCore.js"></script>

<script type="text/javascript" src="../lib/prototype/prototype.js"></script>
<script type="text/javascript" src="../lib/gears/gears_init.js"></script>
<script type="text/javascript" src="../src/ObjectThought.js"></script>

<script type="text/javascript" src="testclasses.js"></script>

<script type="text/javascript">//<![CDATA[

var simpleObject = null;

function setUp()
{
    var store = ObjectStore.getStore("objectthought_unittests");
    store.deleteObjects(SimpleClass);
    simpleObject = new SimpleClass("A Value");
    var validationReport = store.saveObject(simpleObject);
    assertTrue(validationReport.toString(), validationReport.passes());
    assertNotNull(simpleObject.id);
}

function testRetrievingAndUpdatingASimpleObject()
{
    var store = ObjectStore.getStore("objectthought_unittests");
    
    var simpleFromStore = store.getObject(SimpleClass, simpleObject.id);
    assertNotNull("object from store is not null", simpleFromStore);
    assertEquals(simpleFromStore.id, simpleObject.id);
    assertEquals(simpleFromStore.dataValue, "A Value");
    
    simpleFromStore.dataValue = "A New Value";
    var validationReport = store.saveObject(simpleFromStore);
    
    
    simpleFromStore = store.getObject(SimpleClass, simpleObject.id);
    assertNotNull("object from store is not null", simpleFromStore);
    assertEquals(simpleFromStore.id, simpleObject.id);
    assertEquals(simpleFromStore.dataValue, "A New Value");
}

function testRetrievingAllObjectsOfaSingleType()
{
	var store = ObjectStore.getStore("objectthought_unittests");
	var tableLength = 5;
	for (var i = 0; i++ < tableLength; )
	{
    	var validationReport = store.saveObject(new SimpleClass("A Value + " + i));
		assertTrue(validationReport.toString(), validationReport.passes());
	}
	
	var simpleFromStore = store.getObjects(SimpleClass);
	assertNotNull("List of objects should not be null", simpleFromStore);
	assertTrue("Returned object should be an Array", Object.isArray(simpleFromStore));
	assertEquals(simpleFromStore.length, tableLength + 1);
	
	simpleFromStore = simpleFromStore.sortBy(function(test){ return test.dataValue; });
	
	assertEquals(simpleFromStore[0].dataValue, "A Value");
	
	for (var i = 1; i <= tableLength; i++)
	{
		assertEquals(simpleFromStore[i].dataValue, "A Value + " + i);
	}
}

function testAFailingSave()
{
    var store = ObjectStore.getStore("objectthought_unittests");
    
    store.testMode({
        simulatedDatabaseErrors:
        {
            onCommit: function()
            {
                throw new Error("****THIS IS A SIMULATED DATABASE ERROR****");
            }
        },
        
        test: function()
        {
            simpleObject = new SimpleClass("A Value");
            assertNull(simpleObject.id);
        
            try
            {
                var validationReport = store.saveObject(simpleObject);
                assert("we should never get here", false);
            }
            catch(e)
            {
                console.log(e);
                assertEquals("****THIS IS A SIMULATED DATABASE ERROR****", e.message);
            }
        
            assertNull(simpleObject.id);
        }
    });
}

function tearDown()
{
    var store = ObjectStore.getStore("objectthought_unittests");
    
    var originalID = simpleObject.id;
    
    store.deleteObject(simpleObject);
    assertNull("the object's id is null", simpleObject.id);
    var simpleFromStore = store.getObject(SimpleClass, originalID);
    assertNull("object from store is null", simpleFromStore);
	
	store.deleteObjects(SimpleClass);
}

//]]></script>

</head>

<body>
    <p>This is a <a href="http://www.jsunit.net/">JsUnit</a> test page.</p>
    <p>In order to run these tests, please load this html file in JsUnit's <a href="../lib/jsunit/testRunner.html">testRunner.html</a>.</p>
</body>

</html>